import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import data from '../assets/data';


const store = new Vuex.Store({}) // 这里你可能已经有其他 module
const actions = {
  getData({commit, state}, param = {}) {
    let page = param.page;  // 实际项目中会用到分页参数 page
    let scb = param.scb;
    let ecb = param.ecb;
    request().then(res => {
      scb && scb(res);
    }, err => {
      ecb && ecb(err);
    })
  }
}
store.registerModule('vux', { // 名字自己定义
  state: {
    isLoading: false,
    direction: 'forward',
    snsUserInfo: {}
  },
  mutations: {
    updateDirection(state, payload) {
      state.direction = payload.direction
    },
    updateLoadingStatus(state, payload) {
      state.isLoading = payload.isLoading
    },
    updateSnsUserInfo(state, payload) {

      state.snsUserInfo = payload;
    },
    updateUsername(state, payload) {
      state.snsUserInfo.username = payload.username;
    }
  }
})

const getRandomData = (arr, count) => {
  let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
  while (i-- > min) {
    index = Math.floor((i + 1) * Math.random());
    temp = shuffled[index];
    shuffled[index] = shuffled[i];
    shuffled[i] = temp;
  }
  return shuffled.slice(min);
}
const request = () => {
  return new Promise((res, rej) => {
    let requestTime = Math.ceil(((0.1 + Math.random())) * 1000);
    try {
      setTimeout(() => {
        let result = getRandomData(data, 10);
        res(result);
      }, requestTime)
    } catch (e) {
      rej(e);
    }
  })
};
store.registerModule('scroll', { // 名字自己定义
  state: {},
  actions: {
    getData({commit, state}, param = {}) {
      let page = param.page;  // 实际项目中会用到分页参数 page
      let scb = param.scb;
      let ecb = param.ecb;
      request().then(res => {
        scb && scb(res);
      }, err => {
        ecb && ecb(err);
      })
    }
  }
})
export default store
